package com.example.chapter06;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter06.database.ShoppingDBHelper;
import com.example.chapter06.entity.GoodsInfo;
import com.example.chapter06.util.ToastUtil;

public class ShoppingDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "shoppingDetailActivity";
    private int mGoodsId;
    private ImageView iv_detail_goods;
    private TextView tv_detail_price;
    private TextView tv_detail_description;
    private ShoppingDBHelper mDBHelper;
    private TextView tv_count;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_detail);
        iv_detail_goods = findViewById(R.id.iv_detail_goods);
        tv_detail_price = findViewById(R.id.tv_detail_price);
        tv_detail_description = findViewById(R.id.tv_detail_description);


        //更换标题
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("商品详情");
        //将左上角数量显示
        tv_count = findViewById(R.id.tv_count);
        tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));

        //上一个界面传来的goods_id
        mGoodsId = getIntent().getIntExtra("goods_id", -1);
        mDBHelper = ShoppingDBHelper.getInstance(this);




        findViewById(R.id.btn_detail_add_cart).setOnClickListener(this);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_cart).setOnClickListener(this);


    }

    @Override
    protected void onResume() {
        super.onResume();
        initView(mGoodsId);
    }

    private void initView(int goodsId) {
        if (goodsId == -1) {
            Log.e(TAG, "receive goodsId is null");
            return;
        }
        GoodsInfo goods = mDBHelper.queryGoodsInfoById(goodsId);
        Log.d(TAG, goods.toString());
        iv_detail_goods.setImageURI(Uri.parse(goods.getPicPath()));
        tv_detail_price.setText(String.valueOf(goods.getPrice()));
        tv_detail_description.setText(String.valueOf(goods.getDescription()));

    }

    @Override
    public void onClick(View v) {
        //添加到购物车
        if (v.getId() == R.id.btn_detail_add_cart) {
            //将总数量加1,并更新内部的tv_count大小
            tv_count.setText(String.valueOf(++MyApplication.getInstance().goodsCount));
            //插入数据库
            mDBHelper.insertCartInfo(mGoodsId);
            ToastUtil.show(this,"成功添加到购物车");
        }
        if (v.getId() == R.id.iv_back) {
            finish();
        }
        if(v.getId() == R.id.iv_cart){
            Intent intent = new Intent(this,ShoppingCartActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }
}